﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Drawing;
using GreatB2C.Utility;

namespace GreatB2C.Web.Manage.SystemSet.Shop
{
    public partial class TrademarkManage : Inherit.ManagePage
    {
        Business.Trademark BLL = new Business.Trademark( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                LoadData( );
            }
        }

        private void LoadData( )
        {
            // 根据数据库对应的表，映射到相应的操作对象
            DataTable dtData = null;
            if ( Request.QueryString[ "TrademarkType" ] != null && Request.QueryString[ "TrademarkType" ].ToString( ) != "" )
            {
                dtData = BLL.ListTrademarkByType( Convert.ToInt32( Request.QueryString[ "TrademarkType" ].ToString( ) ) );
            }
            else
            {
                dtData = BLL.ListTrademark( );
            }

            DataView dvData = dtData.DefaultView;

            if ( Request.QueryString[ "Action" ] != null && Request.QueryString[ "Action" ].ToString( ) != "" )
            {
                string field = Request.QueryString[ "SearchType" ].ToString( );
                string value = Request.QueryString[ "KeyWord" ].ToString( );

                dvData.RowFilter = field + " like '%" + value + "%' ";
            }

            dvData.Sort = "TrademarkID desc";

            if ( dvData.Count > 0 )
            {
                AspNetPager1.RecordCount = dvData.Count;

                PagedDataSource pds = new PagedDataSource( );
                pds.DataSource = dvData;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                dgData.DataSource = pds;
                dgData.DataBind( );

                dgData.Visible = true;
                litNone.Visible = false;
            }
            else
            {
                dgData.Visible = false;
                litNone.Text = "<div style='width:120px; margin:0 auto; margin-top:20px;'>没 有 任 何 数 据 ！</div>";
            }
        }

        protected void AspNetPager1_PageChanged( object src , EventArgs e )
        {
            LoadData( );
        }

        protected void dgData_ItemDataBound( object sender , DataGridItemEventArgs e )
        {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
            {
                //添加自定义属性，当鼠标移过来时设置该行的背景色,并保存原背景色
                e.Item.Attributes.Add( "onmouseover" , "this.className='tdbgmouseover';" );
                //添加自定义属性，当鼠标移走时还原该行的背景色
                e.Item.Attributes.Add( "onmouseout" , "this.className='tdbg';" );
                //添加双击事件
                e.Item.Attributes.Add( "ondblclick" , "RowDblclick('TrademarkModify.aspx?TrademarkId=" + e.Item.Cells[ 1 ].Text.Trim( ) + "');" );

                if ( e.Item.Cells[ 3 ].Text.Trim( ) == "-1" )
                {
                    e.Item.Cells[ 3 ].Text = "";
                }
                else
                {
                    Business.Dictionary dictBLL = new Business.Dictionary( );
                    Model.Dictionary dictModel = dictBLL.ListByFieldName( DataDefine.Dictionary.TrademarkType.ToString( ) );

                    if ( dictModel != null )
                    {
                        if ( dictModel.FieldValue.Trim( ) != "" )
                        {
                            // 格式 ： 选项名|是否启用值|默认值
                            int i=0;
                            string[] parts = dictModel.FieldValue.ToString( ).Split( '$' );
                            foreach ( string part in parts )
                            {
                                if ( part.IndexOf( "|" ) > 0 )
                                {
                                    string[] choises  = part.Split( '|' );
                                    if ( choises.Length == 3 )
                                    {
                                        if ( i.ToString( ) == e.Item.Cells[ 3 ].Text.Trim( ) )
                                        {
                                            e.Item.Cells[ 3 ].Text = choises[ 0 ];
                                            break;
                                        }

                                        i++;
                                    }
                                }
                            }
                        }
                    }
                }

                LinkButton lbtnOpen = ( LinkButton )e.Item.FindControl( "lbtnOpen" );
                LinkButton lbtnTop = ( LinkButton )e.Item.FindControl( "lbtnTop" );
                LinkButton lbtnRecommand = ( LinkButton )e.Item.FindControl( "lbtnRecommand" );
                LinkButton lbtnDelete = ( LinkButton )e.Item.FindControl( "lbtnDelete" );
                lbtnDelete.Attributes.Add( "onclick" , "return confirm('确定要删除（" + e.Item.Cells[ 2 ].Text.Trim( ) + "）品牌信息吗？');" );

                Label lblOnTop = ( Label )e.Item.FindControl( "lblOnTop" );
                Label lblRecommand = ( Label )e.Item.FindControl( "lblRecommand" );

                if ( Convert.ToBoolean( e.Item.Cells[ 4 ].Text.Trim( ) ) )
                {
                    lbtnOpen.Text = "禁止";
                    lbtnOpen.ForeColor = Color.Red;

                    e.Item.Cells[ 4 ].Text = "<font style='color:green;'>√</font>";
                }
                else
                {
                    lbtnOpen.Text = "启用";
                    lbtnOpen.ForeColor = Color.Green;

                    e.Item.Cells[ 4 ].Text = "<font style='color:red;'>×</font>";
                }

                if ( Convert.ToBoolean( e.Item.Cells[ 7 ].Text.Trim( ) ) )
                {
                    lbtnTop.Text = "解固";
                    lbtnTop.ForeColor = Color.Red;

                    lblOnTop.Visible = true;
                }
                else
                {
                    lbtnTop.Text = "固顶";
                    lbtnTop.ForeColor = Color.Green;

                    lblOnTop.Visible = false;
                }

                if ( Convert.ToBoolean( e.Item.Cells[ 8 ].Text.Trim( ) ) )
                {
                    lbtnRecommand.Text = "解荐";
                    lbtnRecommand.ForeColor = Color.Red;

                    lblRecommand.Visible = true;
                }
                else
                {
                    lbtnRecommand.Text = "推荐";
                    lbtnRecommand.ForeColor = Color.Green;

                    lblRecommand.Visible = false;
                }
            }
        }

        protected void dgData_ItemCommand( object source , DataGridCommandEventArgs e )
        {
            bool isSuccess = false;
            string name = e.Item.Cells[ 2 ].Text.Trim( );
            string id = e.CommandArgument.ToString( );
            if ( id.Trim( ) != "" )
            {
                switch ( e.CommandName )
                {
                    case "Update":
                        Response.Redirect( "TrademarkModify.aspx?TrademarkId=" + id );
                        break;
                    case "Open":
                        isSuccess = BLL.UpdateTrademarkPassed( Convert.ToInt32( id ) , !Convert.ToBoolean( e.Item.Cells[ 6 ].Text.Trim( ) ) );
                        LoadData( );
                        break;
                    case "Top":
                        isSuccess = BLL.UpdateTrademarkTop( Convert.ToInt32( id ) , !Convert.ToBoolean( e.Item.Cells[ 7 ].Text.Trim( ) ) );
                        LoadData( );
                        break;
                    case "Recommand":
                        isSuccess = BLL.UpdateTrademarkElite( Convert.ToInt32( id ) , !Convert.ToBoolean( e.Item.Cells[ 8 ].Text.Trim( ) ) );
                        LoadData( );
                        break;
                    case "Delete":
                        Model.Trademark model = new GreatB2C.Model.Trademark( );
                        model.TrademarkID = Convert.ToInt32( id );
                        isSuccess = BLL.Delete( model );
                        if ( isSuccess )
                        {
                            litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','品牌信息（" + name + "）删除成功！','TrademarkManage.aspx');</script>";
                        }
                        else
                        {
                            litScript.Text = "<script type='text/javascript'>ShowError('操作失败','品牌信息（" + name + "）删除失败！');</script>";
                        }
                        break;
                }
            }
        }
    }
}
